<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="出入库类型" prop="outInStockType">
      <el-input v-model="dataForm.outInStockType" placeholder="出入库类型"></el-input>
    </el-form-item>
    <el-form-item label="产品编码" prop="productionNum">
      <el-input v-model="dataForm.productionNum" placeholder="产品编码"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="productAmount">
      <el-input v-model="dataForm.productAmount" placeholder="数量"></el-input>
    </el-form-item>
    <el-form-item label="仓库编号" prop="warehouseNum">
      <el-input v-model="dataForm.warehouseNum" placeholder="仓库编号"></el-input>
    </el-form-item>
    <el-form-item label="出入库日期" prop="outInStockDate">
      <el-input v-model="dataForm.outInStockDate" placeholder="出入库日期"></el-input>
    </el-form-item>
    <el-form-item label="说明" prop="comments">
      <el-input v-model="dataForm.comments" placeholder="说明"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          inStockNum: 0,
          outInStockType: '',
          productionNum: '',
          productAmount: '',
          warehouseNum: '',
          outInStockDate: '',
          comments: ''
        },
        dataRule: {
          outInStockType: [
            { required: true, message: '出入库类型不能为空', trigger: 'blur' }
          ],
          productionNum: [
            { required: true, message: '产品编码不能为空', trigger: 'blur' }
          ],
          productAmount: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          warehouseNum: [
            { required: true, message: '仓库编号不能为空', trigger: 'blur' }
          ],
          outInStockDate: [
            { required: true, message: '出入库日期不能为空', trigger: 'blur' }
          ],
          comments: [
            { required: true, message: '说明不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.inStockNum = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.inStockNum) {
            this.$http({
              url: this.$http.adornUrl(`/stock/messtockrecordin/info/${this.dataForm.inStockNum}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.outInStockType = data.messtockrecordin.outInStockType
                this.dataForm.productionNum = data.messtockrecordin.productionNum
                this.dataForm.productAmount = data.messtockrecordin.productAmount
                this.dataForm.warehouseNum = data.messtockrecordin.warehouseNum
                this.dataForm.outInStockDate = data.messtockrecordin.outInStockDate
                this.dataForm.comments = data.messtockrecordin.comments
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/stock/messtockrecordin/${!this.dataForm.inStockNum ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'inStockNum': this.dataForm.inStockNum || undefined,
                'outInStockType': this.dataForm.outInStockType,
                'productionNum': this.dataForm.productionNum,
                'productAmount': this.dataForm.productAmount,
                'warehouseNum': this.dataForm.warehouseNum,
                'outInStockDate': this.dataForm.outInStockDate,
                'comments': this.dataForm.comments
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
